package com.dhcc.sdc.busi.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.busi.dao.SdcQcRuleDatasourceMapper;
import com.dhcc.sdc.busi.entity.SdcQcRuleDatasourceEntity;
import com.dhcc.sdc.busi.service.ISdcQcRuleDatasourceService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


/**
 * 质控-数据源表数据服务层接口
 * 
 * @ClassName: SdcQcRuleDatasourceServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-07-26 14:24:20
 */
@Service("sdcQcRuleDatasourceService")
public class SdcQcRuleDatasourceServiceImpl extends BaseServiceImpl<SdcQcRuleDatasourceMapper, SdcQcRuleDatasourceEntity> implements ISdcQcRuleDatasourceService {

	@Override
	public List<SdcQcRuleDatasourceEntity> page(Page<SdcQcRuleDatasourceEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdcQcRuleDatasourceEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public List<SdcQcRuleDatasourceEntity> selectByExpression(Map<String, Object> params) {
		List<SdcQcRuleDatasourceEntity> res = new ArrayList<>();

		String compare = (String) params.get("compare");
		if (Objects.equals("all", compare)){
			res = this.baseMapper.selectList(null);
		}else {
			String ruleExpressions = "";
			if (!StringUtils.isEmpty(params.get("ruleExpressions"))){
				try {
					ruleExpressions = java.net.URLDecoder.decode((String) params.get("ruleExpressions"), "UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				List<String> list = handleRuleExpressions(ruleExpressions);
				res = this.baseMapper.selectByExpressions(list);
			}
		}
		return res;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importItem(List<List<Object>> datasourceAll) {
		for (int i = 1; i < datasourceAll.size(); i++){
			List<Object> ls = datasourceAll.get(i);
			//代码
			String code = ls.get(0).toString();
			//数据源
			String dataSource = ls.get(1).toString();
			//具体取值项目
			String dataSourceItem = ls.get(2).toString();
			//是否需要传参
			Integer havePar = CommonUtil.equals(ls.get(3),"是")?1:0;

			SdcQcRuleDatasourceEntity sdcQcRuleDatasourceEntity = new SdcQcRuleDatasourceEntity();
			sdcQcRuleDatasourceEntity.setCode(code);
			sdcQcRuleDatasourceEntity = this.baseMapper.selectOne(sdcQcRuleDatasourceEntity);
			if (sdcQcRuleDatasourceEntity == null){
				sdcQcRuleDatasourceEntity = new SdcQcRuleDatasourceEntity();
				sdcQcRuleDatasourceEntity.setCode(code);
				sdcQcRuleDatasourceEntity.setDataSource(dataSource);
				sdcQcRuleDatasourceEntity.setDataSourceItem(dataSourceItem);
				sdcQcRuleDatasourceEntity.setHavePar(havePar);

				sdcQcRuleDatasourceEntity.insert();
			}else {
				sdcQcRuleDatasourceEntity.setDataSource(dataSource);
				sdcQcRuleDatasourceEntity.setDataSourceItem(dataSourceItem);
				sdcQcRuleDatasourceEntity.setHavePar(havePar);

				sdcQcRuleDatasourceEntity.updateAllColumnById();
			}
		}
	}

	@Override
	public JSONObject getDataSourceMap() {
		JSONObject jsObj = new JSONObject();
		jsObj.put("","");
		List<SdcQcRuleDatasourceEntity> list = this.baseMapper.selectList(null);
		for (SdcQcRuleDatasourceEntity sdcQcRuleDatasourceEntity : list) {
			jsObj.put(sdcQcRuleDatasourceEntity.getId().toString(), sdcQcRuleDatasourceEntity.getDataSource());
		}
		return jsObj;
	}

	@Override
	public JSONObject getDataSourceItemMap() {
		JSONObject jsObj = new JSONObject();
		jsObj.put("","");
		List<SdcQcRuleDatasourceEntity> list = this.baseMapper.selectList(null);
		for (SdcQcRuleDatasourceEntity sdcQcRuleDatasourceEntity : list) {
			jsObj.put(sdcQcRuleDatasourceEntity.getId().toString(), sdcQcRuleDatasourceEntity.getDataSourceItem());
		}
		return jsObj;
	}

	private List<String> handleRuleExpressions(String ruleExpressions) {
		//小括号及其里面内容
		String p1 = "\\(.*?\\)";
		List<String> res = new ArrayList<>();
		Pattern p = Pattern.compile(p1);
		Matcher m = p.matcher(ruleExpressions);
		while (m.find()){
			String ruleExpression = m.group().substring(1, m.group().length() - 1);
			res.add(ruleExpression);
		}
		return res;
	}

}